package com.pb.b2b.bean;

import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.pb.b2b.common.bean.UserSessionBean;
import com.pb.b2b.util.FacesUtils;

/**
 * @author apinar2
 */
@Component
@Scope("view")
@ManagedBean(name = "mainTemplateBean")
public class MainTemplateBean implements Serializable {
	private static final long serialVersionUID = 1L;
	protected final String RETURN_BACK = "back";
	protected final String CRUMBS_SEPERATOR = ">";

	protected boolean showTitle = true;
	protected boolean showBackButton = true;
	protected boolean showCrumbs = true;

	protected String pageTitle;
	protected String pageTitleKey;
	protected String pageDescription;
	protected String pageDescriptionHeader;
	protected String crumbsRoot;
	protected String crumbsLast;
	protected String pagePath = "";

	/* METHOD */

	@PostConstruct
	public void init() {
		initHeader();
	}

	public void initHeader() {
		String path = FacesUtils.getViewId();
		ResourceBundle bundle = ResourceBundle.getBundle("resources.islemler", getLocale());

		pagePath = formatPagePath(path);
		readPageTitleFromBundle(bundle);
		// readPageDescriptionHeaderFromBundle(bundle);
		// readPageDescriptionFromBundle(bundle);
		readCrumbsLineFromBundle(bundle);
		readShowBackButtonFromBundle(bundle);
		// readAddFavoriteButtonFromBundle(bundle);
		// readHelpLinkFromBundle(bundle);
	}

	private Locale getLocale() {
		UserSessionBean userSession = FacesUtils.getUserSession();
		Locale locale = new Locale("TR");
		if (userSession != null && userSession.getLocale() != null) {
			locale = userSession.getLocale();
		}

		return locale;
	}

	private String formatPagePath(String pagePath) {
		int index = pagePath.lastIndexOf(".");
		pagePath = pagePath.substring(1, index); // ilk "/" atilir
		pagePath = pagePath.replace('/', '.');
		return pagePath;
	}

	private void readPageTitleFromBundle(ResourceBundle resourceBundle) {
		this.pageTitleKey = getTitleKey();
		String pageTitle = "";

		pageTitle = (FacesUtils.getResourceBundleString(resourceBundle, this.pageTitleKey, null));

		if (this.pageTitleKey.equals(pageTitle)) { // bulunamadi, key geri
			// dondu
			setPageTitle(null);
			return;
		}
		setPageTitle(pageTitle);
	}

	private String getTitleKey() {
		String key = "";
		String path = this.pagePath;

		if (path.endsWith("Confirm")) {
			key = "confirm.title";

		} else if (path.endsWith("End")) {
			key = "end.title";

		} else {
			key = path + "." + "title";
		}

		return key;
	}

	private void readCrumbsLineFromBundle(ResourceBundle resourceBundle) {
		String path = this.pagePath;

		String key;
		String crumbsLine;
		key = path + "." + "crumbs";
		crumbsLine = (FacesUtils.getResourceBundleString(resourceBundle, key, null));
		if (key.equals(crumbsLine)) {
			setCrumbsLast(null);
			setCrumbsRoot(null);
			return;
		}
		String root = "";
		String last = "";
		if (crumbsLine != null) {
			int index = crumbsLine.lastIndexOf(CRUMBS_SEPERATOR);
			if (index != -1) {
				if (index < (crumbsLine.length() - 1)) {
					last = crumbsLine.substring(index + 1);
				}
				root = crumbsLine.substring(0, index);
			}
		}
		setCrumbsLast(last);
		setCrumbsRoot(root);
	}

	private void readShowBackButtonFromBundle(ResourceBundle bundle) {
		String key = this.pagePath + "." + "showBackButton";
		String showCrumbs = (FacesUtils.getResourceBundleString(bundle, key, null));
		if (showCrumbs.equalsIgnoreCase("false")) {
			this.showBackButton = false;
		}
	}

	public String previousPageAction() {
		return RETURN_BACK;
	}

	public String homeAction() {
		return "home";
	}

	public String categoryAction() {
		return "category";
	}

	public String productAction() {
		return "product";
	}

	public String customerAction() {
		return "customer";
	}

	public String orderAction() {
		return "order";
	}

	public String settingsAction() {
		return "settings";
	}

	public String logoutAction() {
		return "logout";
	}

	/* GETTER-SETTER */
	public boolean isShowTitle() {
		return showTitle;
	}

	public void setShowTitle(boolean showTitle) {
		this.showTitle = showTitle;
	}

	public boolean isShowBackButton() {
		return showBackButton;
	}

	public void setShowBackButton(boolean showBackButton) {
		this.showBackButton = showBackButton;
	}

	public boolean isShowCrumbs() {
		return showCrumbs;
	}

	public void setShowCrumbs(boolean showCrumbs) {
		this.showCrumbs = showCrumbs;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public String getPageTitleKey() {
		return pageTitleKey;
	}

	public void setPageTitleKey(String pageTitleKey) {
		this.pageTitleKey = pageTitleKey;
	}

	public String getPageDescription() {
		return pageDescription;
	}

	public void setPageDescription(String pageDescription) {
		this.pageDescription = pageDescription;
	}

	public String getPageDescriptionHeader() {
		return pageDescriptionHeader;
	}

	public void setPageDescriptionHeader(String pageDescriptionHeader) {
		this.pageDescriptionHeader = pageDescriptionHeader;
	}

	public String getCrumbsRoot() {
		return crumbsRoot;
	}

	public void setCrumbsRoot(String crumbsRoot) {
		this.crumbsRoot = crumbsRoot;
	}

	public String getCrumbsLast() {
		return crumbsLast;
	}

	public void setCrumbsLast(String crumbsLast) {
		this.crumbsLast = crumbsLast;
	}

	public String getPagePath() {
		return pagePath;
	}

	public void setPagePath(String pagePath) {
		this.pagePath = pagePath;
	}

}
